package org.oak.configuration;

import org.oak.filter.AuthenticationFilter;
import org.oak.filter.CharacterEncodingFilter;
import org.oak.filter.LoggingFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * org.oak.configuration.FilterConfig
 *
 * @author ZRS
 * @version V1.0
 * @since 2025/10/30 17:57
 */
@Configuration
public class FilterConfig {


    @Bean
    public FilterRegistrationBean<LoggingFilter> loggingFilter() {
        FilterRegistrationBean<LoggingFilter> registrationBean =
                new FilterRegistrationBean<>();

        registrationBean.setFilter(new LoggingFilter());

        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(0); // 设置过滤器执行顺序


        return registrationBean;
    }

    @Bean("selfCharacterEncodingFilter")
    public FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilter() {
        FilterRegistrationBean<CharacterEncodingFilter> registrationBean =
                new FilterRegistrationBean<>();

        registrationBean.setFilter(new CharacterEncodingFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(10); // 设置过滤器执行顺序

        registrationBean.addInitParameter("encoding", "UTF-8");

        return registrationBean;
    }


    @Bean
    public FilterRegistrationBean<AuthenticationFilter> authenticationFilter() {
        FilterRegistrationBean<AuthenticationFilter> registrationBean =
                new FilterRegistrationBean<>();

        registrationBean.setFilter(new AuthenticationFilter());
        registrationBean.addUrlPatterns("/api/*");

        registrationBean.setOrder(20);

        return registrationBean;
    }
}